package com.zhsj.OkeHomeConsumption.utils;



import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: JsonUtils
 * @Description: TODO google json 解析工具类
 * @author zhangpeisen
 * @date 2016年9月13日 下午3:41:22
 */
public class JsonUtils {
	private static Gson gson = new Gson();

	public static <T> T object(String json, Class<T> classOfT) {
		return gson.fromJson(json, classOfT);
	}

	public static <T> String toJson(Class<T> param) {
		return gson.toJson(param);
	}

	/**
	 * 转成list 泛型在编译期类型被擦除导致报错
	 * 
	 * @param gsonString
	 * @param cls
	 * @return
	 */
	public static <T> List<T> GsonToList(String gsonString, Class<T> cls) {
		List<T> list = null;
		if (gson != null) {
			list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
			}.getType());
		}
		return list;
	}

	/**
	 * 转成list 解决泛型问题
	 * 
	 * @param json
	 * @param cls
	 * @param <T>
	 * @return
	 */
	public <T> List<T> jsonToList(String json, Class<T> cls) {
		Gson gson = new Gson();
		List<T> list = new ArrayList<T>();
		JsonArray array = new JsonParser().parse(json).getAsJsonArray();
		for (final JsonElement elem : array) {
			list.add(gson.fromJson(elem, cls));
		}
		return list;
	}

	/**
	 * 转成list中有map的
	 * 
	 * @param gsonString
	 * @return
	 */
	public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) {
		List<Map<String, T>> list = null;
		if (gson != null) {
			list = gson.fromJson(gsonString, new TypeToken<List<Map<String, T>>>() {
			}.getType());
		}
		return list;
	}

	/**
	 * 转成map的
	 * 
	 * @param gsonString
	 * @return
	 */
	public static <T> Map<String, T> GsonToMaps(String gsonString) {
		Map<String, T> map = null;
		if (gson != null) {
			map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
			}.getType());
		}
		return map;
	}




	// json 串 转List<T>
	public static <T> List<T> jsonToList(Class<T> tClass, String json) {

		ParameterizedTypeImpl parameterizedTypeList = new ParameterizedTypeImpl(List.class, new Class[]{tClass});
		if (!TextUtils.isEmpty(json)) {
			if(json != null){
				return gson.fromJson(json, parameterizedTypeList);
			}
		}
		return null;
	}


	/**把数据源HashMap转换成json
	 * @param map
	 */
	public static String hashMapToJson(HashMap map) {
		String string = "{";
		for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
			Map.Entry e = (Map.Entry) it.next();
			string += "'" + e.getKey() + "':";
			string += "'" + e.getValue() + "',";
		}
		string = string.substring(0, string.lastIndexOf(","));
		string += "}";
		return string;
	}


	private static class ParameterizedTypeImpl implements ParameterizedType {

		private final Class raw;
		private final Type[] args;

		public ParameterizedTypeImpl(Class raw, Type[] args) {
			this.raw = raw;
			this.args = args != null ? args : new Type[0];
		}

		@Override
		public Type[] getActualTypeArguments() {
			return args;
		}

		@Override
		public Type getRawType() {
			return raw;
		}

		@Override
		public Type getOwnerType() {
			return null;
		}
	}

}